<?php

defined('STDERR') || define('STDERR', fopen('php://stderr', 'w'));

/**
 *
 * Severity levels, as defined in RFC 3164 http://www.faqs.org/rfcs/rfc3164.html
 * @see watchdog()
 * @see watchdog_severity_levels()
 */
define('WATCHDOG_EMERG',    0);	// Emergency: system is unusable
define('WATCHDOG_ALERT',    1);	// Alert: action must be taken immediately
define('WATCHDOG_CRITICAL', 2); // Critical: critical conditions
define('WATCHDOG_ERROR',    3); // Error: error conditions
define('WATCHDOG_WARNING',  4);	// Warning: warning conditions
define('WATCHDOG_NOTICE',   5);	// Notice: normal but significant condition
define('WATCHDOG_INFO',     6);	// Informational: informational messages
define('WATCHDOG_DEBUG',    7);	// Debug: debug-level messages

define('WATCHDOG_DEFAULT_TYPE',        'Watchdog message');
define('WATCHDOG_DEFAULT_DEBUG_LEVEL', WATCHDOG_DEBUG);
define('WATCHDOG_DEFAULT_LOGS',        null);

$logs        = WATCHDOG_DEFAULT_LOGS;
$debug_level = WATCHDOG_DEFAULT_DEBUG_LEVEL;

/**
 * Print to screen and to logfile a message
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 *
 */
function watchdog($message,
                  $severity = WATCHDOG_NOTICE,
                  $type = WATCHDOG_DEFAULT_TYPE,
                  array $variables = array()) {
  global $debug_level;
  if ($severity <= $debug_level) {
    watchdog_log($message, $severity, $type, $variables);
    watchdog_echo($message, $severity, $type, $variables);
  }
}

/**
 * Print a log message on a the log file
 *
 * @global resource $logs
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 */
function watchdog_log($message,
                      $severity = WATCHDOG_NOTICE,
                      $type = WATCHDOG_DEFAULT_TYPE,
                      array $variables = array()) {
  global $logs;
  if (is_resource($logs)) {
    _watchdog_fprintf($logs, watchdog_create_log_message($message,
                                                         $severity,
                                                         $type,
                                                         $variables));
  }
}

/**
 * Print a log message on the screen
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 */
function watchdog_echo($message,
                       $severity = WATCHDOG_NOTICE,
                       $type = WATCHDOG_DEFAULT_TYPE,
                       array $variables = array()) {
  _watchdog_fprintf(STDERR, watchdog_create_log_message($message,
                                                        $severity,
                                                        $type,
                                                        $variables));
}

/**
 * Helper function
 *
 * @param resource $stream
 * @param array $log_message
 */
function _watchdog_fprintf($stream, array $log_message) {
  fprintf($stream, "%s - %s - %s\n",
          date('Y/m/d H:i:s'),
          ucfirst($log_message['severity']),
          $log_message['message']);
}

/**
 * Create an array structure containing info about logging
 *
 * @param string $message
 * @param int $severity
 * @param string $type
 * @param variables $variables
 * @return array
 */
function watchdog_create_log_message($message = 'Error',
                                     $severity = WATCHDOG_NOTICE,
                                     $type = WATCHDOG_DEFAULT_TYPE,
                                     array $variables = array()) {
  return array('message'   => $message,
               'severity'  => watchdog_get_severity_level($severity),
               'type'      => $type,
               'variables' => $variables,
               'timestamp' => time());
}

function watchdog_init($custom_logs = WATCHDOG_DEFAULT_LOGS,
                       $custom_debug_level = WATCHDOG_DEFAULT_DEBUG_LEVEL) {
  global $logs, $debug_level;
  $logs = $custom_logs;
  $debug_level = $custom_debug_level;
}

/**
 * Return all the watchdog security levels
 *
 * @return array
 */
function watchdog_severity_levels() {
  return array(WATCHDOG_EMERG    => 'emergency',
               WATCHDOG_ALERT    => 'alert',
               WATCHDOG_CRITICAL => 'critical',
               WATCHDOG_ERROR    => 'error',
               WATCHDOG_WARNING  => 'warning',
               WATCHDOG_NOTICE   => 'notice',
               WATCHDOG_INFO     => 'info',
               WATCHDOG_DEBUG    => 'debug');
}

/**
 * Return the name of a severity level id
 *
 * @param int $severity
 * @return string
 *   On failure return null
 */
function watchdog_get_severity_level($severity) {
  $levels = watchdog_severity_levels();
  if (isset($levels[$severity])) {
    return $levels[$severity];
  }
  return null;
}
